import DataCenter from "./DataCenter";
import CommunicationManager from "./CommunicationManager";
import { ACK_Type, ProductID, DeviceModelID, Topic_Loop, Topic_Master, Topic_Strategy, Topic_Tunnel, Topic_Report } from "./LogicConstant";
import { commonMakeMqttRequest } from "./Utils";
import Config from "react-native-config";
import { formatterSendTopic } from "./TopicUtil";
import { getAppVersion } from "../Http/HttpService";
import UpgradeLibrary from "../libs/Upgrade";
import XLog from "../libs/XLog";
import { versionCompare } from "../common/Utils/Utils";
import DeviceInfo from 'react-native-device-info';
import {MQTT_ASYNC_INTERVAL} from "./LogicConstant";

export default class AsyncDataManager {
    static mqttAsyncTimer = null;
    
    static installAsyncManagerLogic() {
        this.mqttAsyncTimer = setInterval(async()=>{
            //如果是主Master
            if(DataCenter.isMaster) {
                //
                CommunicationManager.send2Remote(formatterSendTopic(Topic_Loop), commonMakeMqttRequest(ACK_Type.ACK, {tunnelId: DataCenter.tunnelId, tunnelType: DataCenter.tunnelType}, Topic_Loop.method));
                CommunicationManager.send2Remote(formatterSendTopic(Topic_Tunnel), commonMakeMqttRequest(ACK_Type.ACK, {tunnelId: DataCenter.tunnelId, tunnelType: DataCenter.tunnelType}, Topic_Tunnel.method));
                CommunicationManager.send2Remote(formatterSendTopic(Topic_Strategy), commonMakeMqttRequest(ACK_Type.ACK, null, Topic_Strategy.method));
            }
                
            CommunicationManager.send2Remote(formatterSendTopic(Topic_Master), commonMakeMqttRequest(ACK_Type.ACK, {gateIp: DataCenter.machineIp}, Topic_Master.method));

            CommunicationManager.send2Remote(formatterSendTopic(Topic_Report), commonMakeMqttRequest(ACK_Type.ACK, await this.getDeviceInfo(), Topic_Report.method));

            try{
                if(DataCenter.serverIp) {
                    const{success, data} = await getAppVersion();
                    if(success) {
                        if(versionCompare(DeviceInfo.getBuildNumber(), data.ver) < 0) {
                            await UpgradeLibrary.doNewVersionUpdate(data.url, false);
                        }
                    } else {
                        XLog.e("getAppVersion error");
                    }
                }
            }catch(error) {
                XLog.e("getAppVersion exception", error);
            }
        }, MQTT_ASYNC_INTERVAL/1);
    }

    static uninstallAsyncManagerLogic() {
        if(this.mqttAsyncTimer) {
            clearInterval(this.mqttAsyncTimer);
            this.mqttAsyncTimer = null;
        }
    }

    static async getDeviceInfo(){
        return {
            "freeDisk": await DeviceInfo.getFreeDiskStorage(),
            "totalDisk": await DeviceInfo.getTotalDiskCapacity(),
            "totalMemory": await DeviceInfo.getTotalMemory(),
            "usedMemory": await DeviceInfo.getUsedMemory(),
            "productId": ProductID.GateWay,
            "identifierId": DeviceModelID.GateWay,
        }
    }

    static async getDeviceConstInfo(){
        let macAddress = null;
        await DeviceInfo.getMacAddress().then((mac) => {
            macAddress = mac;
          });

        return {
            "brand": DeviceInfo.getBrand(),
            "manufacturer": await DeviceInfo.getManufacturer(),
            "deviceName": await DeviceInfo.getDeviceName(),
            "mac": macAddress,
            "version": DeviceInfo.getSystemVersion(),
            "isTablet": DeviceInfo.isTablet() ? 1 : 0,
        }
    }
}